package com.ysxsoft.wdg.ui.activity.center_zy;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.classic.common.MultipleStatusView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.ysxsoft.wdg.R;
import com.ysxsoft.wdg.adapter.home.ZyGoodsAdapter;
import com.ysxsoft.wdg.base.BaseActivity;
import com.ysxsoft.wdg.bean.BaseBean;
import com.ysxsoft.wdg.bean.zy.GoodsBean;
import com.ysxsoft.wdg.net.CallbackCode;
import com.ysxsoft.wdg.util.ToastUtils;
import com.ysxsoft.wdg.util.ViewUtils;
import com.ysxsoft.wdg.util.sp.SharePrefUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * 淘宝客搜索结果
 */
public class ZySearchListActivity extends BaseActivity implements OnRefreshListener,OnLoadmoreListener{

    public static void start(Context context,String keyword) {
        Intent intent = new Intent(context, ZySearchListActivity.class);
        intent.putExtra("keyword",keyword);
        context.startActivity(intent);
    }


    @BindView(R.id.title_tv_r)
    TextView titleTvR;
    @BindView(R.id.topView)
    View topView;
    @BindView(R.id.title_content)
    EditText titleContent;
    @BindView(R.id.tv0)
    TextView tv0;
    @BindView(R.id.tv1)
    TextView tv1;
    @BindView(R.id.tv2)
    TextView tv2;
    @BindView(R.id.tv3)
    TextView tv3;
    @BindView(R.id.iv3)
    ImageView iv3;
    @BindView(R.id.recyclerview2)
    RecyclerView recyclerview2;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.multStatusView)
    MultipleStatusView multStatusView;

    private TextView[] textViews;

    private ZyGoodsAdapter mAdapter;
    private String keyword = "",uid ="";
    private int page =1, xu =0;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_tb_search_list;
    }

    @Override
    protected void initData() {
        initStatusBar(topView, false);
        titleTvR.setText("");
        titleContent.setCursorVisible(false);
        titleContent.setFocusable(false);
        titleContent.setFocusableInTouchMode(false);

        uid = SharePrefUtils.getUserId();

        textViews = new TextView[]{tv0, tv1, tv2, tv3};

        Intent intent = getIntent();
        keyword = intent.getStringExtra("keyword");
        titleContent.setText(keyword);


        recyclerview2.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        mAdapter = new ZyGoodsAdapter(R.layout.item_goods);
        recyclerview2.setAdapter(mAdapter);

        setSelect(0);
    }


    @Override
    protected void setListener() {
        refreshLayout.setOnLoadmoreListener(this);
        refreshLayout.setOnRefreshListener(this);

        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                GoodsDetailActivity.start(mContext,mAdapter.getItem(position).getId());
            }
        });
    }


    @OnClick({R.id.title_content,R.id.title_finish})
    public void onViewClicked1(View view) {
        switch (view.getId()) {
            case R.id.title_content:
            case R.id.title_finish:
                finish();
                break;
        }
    }
    @OnClick({ R.id.tv0, R.id.tv1, R.id.tv2, R.id.tv3})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv0:
                iv3.setImageResource(R.mipmap.xu_default);
                xu = 0;
                setSelect(0);
                break;
            case R.id.tv1:
                iv3.setImageResource(R.mipmap.xu_default);
                xu = 1;
                setSelect(1);
                break;
            case R.id.tv2:
                iv3.setImageResource(R.mipmap.xu_default);
                xu = 2;
                setSelect(2);
                break;
            case R.id.tv3://3倒序4正序
                xu = (xu == 3) ? 4 :3;
                iv3.setImageResource(xu == 4?R.mipmap.xu_up:R.mipmap.xu_down);
                setSelect(3);
                break;
        }

    }

    private void setSelect(int position) {

        for (int i = 0; i < textViews.length; i++) {
            textViews[i].setSelected(i == position);
        }
        onRefresh(refreshLayout);
    }


    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        refreshlayout.finishRefresh(500);
        page = 1;
        mAdapter.clear();
        getList();
    }



    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        refreshlayout.finishLoadmore(500);

        page++;
        getList();

    }

    private void getList() {
        mApiHelper.getZySearchList(page + "", keyword, xu + "",uid, new Observer<BaseBean<List<GoodsBean>>>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(BaseBean<List<GoodsBean>> baseBean) {
                if (baseBean.getCode().equals(CallbackCode.SUCCESS)){
                    try {
                        multStatusView.showContent();
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    if (page == 1){
                        mAdapter.setNewData(baseBean.getData());
                    }else {
                        mAdapter.addData(baseBean.getData());
                    }
                }else {
                    if (page == 1){
                        multStatusView.showEmpty();
                    }else {
                        multStatusView.showContent();
                        ToastUtils.showToast(baseBean.getMs());
                    }
                }
            }

            @Override
            public void onError(Throwable e) {
                multStatusView.showError();
            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    protected void onPause() {
        super.onPause();
        ViewUtils.closeKeyboard(this);
    }
}
